package cn.e2win.biz.notice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.e2win.biz.comm.ViewExcel;
import cn.e2win.biz.dbo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import org.springframework.web.servlet.ModelAndView;


@Controller
@Scope("request")
@RequestMapping("/notice")
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseService initService() {
		return noticeService;
	}

	@RequestMapping("toNoticeEditor/{id}")
	public String toNoticeEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		DsEmp empty = new DsEmp(id);
		String clazz = DsEmp.class.getSimpleName();
		Map<String, Object> obj = noticeService.getEntity(empty);
		modelMap.put(OBJECT, obj);
		List<SysLabel> labelIndustryList = commonService.getSysLabelList(empty, "labelIndustry");
		List<SysLabel> labelFocusOnList = commonService.getSysLabelList(empty,"labelFocusOn");	
		List<SysLabel> labelHelpForList = commonService.getSysLabelList(empty,"labelHelpFor");						
		modelMap.put("labelIndustryList", labelIndustryList);
		modelMap.put("labelFocusOnList", labelFocusOnList);
		modelMap.put("labelHelpForList", labelHelpForList);

		List<SysLabel> allLabelIndustryList = commonService.getSysLabelList(clazz,"labelIndustry");
		List<SysLabel> allLabelFocusOnList = commonService.getSysLabelList(clazz,"labelFocusOn");
		List<SysLabel> allLabelHelpForList = commonService.getSysLabelList(clazz,"labelHelpFor");
		modelMap.put("allLabelIndustryList", allLabelIndustryList);
		modelMap.put("allLabelFocusOnList", allLabelFocusOnList);
		modelMap.put("allLabelHelpForList", allLabelHelpForList);
		return "notice/noticeEditor";
	}

	@RequestMapping("editNotice")
	@ResponseBody
	public Result editNotice(HttpServletRequest request,@ModelAttribute Notice obj,ModelMap modelMap) throws MyException{
		Result r = new Result();
		noticeService.editNotice(obj);
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}


	@RequestMapping("noticePager")
	public String noticePager(HttpServletRequest request, ModelMap modelMap){
		pager.getScMap().put("toUserId",pager.getCurrUser().getId()+"");
		noticeService.pagerNotice(pager);
		modelMap.put("pager", pager);
		return "notice/noticePager";
	}

	@RequestMapping("noticePagerExcel")
	public ModelAndView customerPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",noticePager(request,modelMap));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}


	@RequestMapping("readMessage/{id}")
	public String readMessage(HttpServletRequest request, ModelMap modelMap,@PathVariable(ID)Integer id) throws MyException {
		Notice notice = (Notice) noticeService.getEntity(Notice.class,id);
		if(notice.getToUserId().equals(pager.getCurrUser().getId())){
			notice.setReadStatus("read");
			noticeService.edit(notice);
		}else{
			throw new MyException("您没有权限阅读该消息");
		}
		String entityType = notice.getEntityType();
		if(entityType.equals(DsEmp.class.getSimpleName())){
			return "redirect:/m/dsDetail.do?id="+notice.getEntityId();
		}else if(entityType.equals(CyzjOrg.class.getSimpleName())){
			return "redirect:/m/cyzjDetail.do?id="+notice.getEntityId();
		}else if(entityType.equals(Activity.class.getSimpleName())){
			return "redirect:/m/activityDetail.do?id="+notice.getEntityId();
		}
		return "redirect:/m/index.do";
	}
	


}
